package com.example.mapper;

import com.example.pojo.Leave;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LeaveMapper {

    @Select("SELECT * FROM leaves WHERE employeeId = #{employeeId}")
    List<Leave> getLeavesByEmployeeId(@Param("employeeId") String employeeId);

    @Insert("INSERT INTO leaves (employeeId, leaveType, startDate, endDate, reason, status) VALUES (#{employeeId}, #{leaveType}, #{startDate}, #{endDate}, #{reason}, 'approved')")
    void applyLeave(Leave leave);

    @Select("SELECT * FROM leaves WHERE leaveId = #{leaveId}")
    Leave getLeaveById(@Param("leaveId") String leaveId);

    @Select("SELECT COALESCE(SUM(DATEDIFF(LEAST(endDate, LAST_DAY(CONCAT(#{year}, '-', #{month}, '-01'))), GREATEST(startDate, CONCAT(#{year}, '-', #{month}, '-01'))) + 1), 0) " +
            "FROM leaves WHERE employeeId = #{employeeId} AND status = 'approved' AND " +
            "(YEAR(startDate) = #{year} AND MONTH(startDate) = #{month} OR YEAR(endDate) = #{year} AND MONTH(endDate) = #{month})")
    Integer getApprovedLeaveDaysForMonth(@Param("employeeId") String employeeId, @Param("year") int year, @Param("month") int month);

}
